package RockPaperScissors.GameFlow
{
    import RockPaperScissors.ProcessorStep;
    import RockPaperScissors.Utilities;
    
    import com.adobe.serialization.json.*;
    
    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.http.HTTPService;

    public dynamic class Introductions extends ProcessorStep
    {
        var results = {"contestant1":null, "contestant2":null};
        
        public function Introductions(onComplete:Function=null, onFail:Function=null)
        {
            super(onComplete, onFail);
            this.setupProcessorStep(onComplete, onFail);
        }
        
        public override function Process(data)
        {
            data["contestant1"]["dynamite_count"] = get_dynamite_count(data["round_count"]);
            data["contestant2"]["dynamite_count"] = get_dynamite_count(data["round_count"]);
            
            //Get the last round's results
            var contestant1data = {"id":"contestant1", "round_count":data["round_count"]};
            
            var service:HTTPService = new HTTPService();
            
            var contestant1_uri = data["contestant1"]["uri"] + "/introductions";
            service.url = contestant1_uri;
            service.addEventListener(FaultEvent.FAULT, onFail);
            service.addEventListener(ResultEvent.RESULT, 
                Utilities.createCallBack(this.onComplete, {"contestant":"contestant1", "data":data, "context":this}));
            service.method = "POST";
            service.send({"data":JSON.encode(contestant1data)});
            
            var contestant2data = {"id":"contestant2", "round_count":data["round_count"]};
            service = new HTTPService();
            var contestant2_uri = data["contestant2"]["uri"] + "/introductions";
            service.url = contestant2_uri;
            service.addEventListener(FaultEvent.FAULT, onFail);
            service.addEventListener(ResultEvent.RESULT, 
                Utilities.createCallBack(this.onComplete, {"contestant":"contestant2", "data":data, "context":this}));
            service.method = "POST";
            service.send({"data":JSON.encode(contestant2data)});
        }
        
        ///Figures out a good starting amount of dynamite given a 
        ///number of rounds.
        public function get_dynamite_count(round_count)
        {
            var rounds:int = (round_count == "") ? 0 : int(round_count);
            var dynamite_count:int = rounds * .05;
            return dynamite_count;
        }
        
        public function onComplete(event:ResultEvent, data)
        {
            var result = JSON.decode(String(event.result));
            var contestant_name:String = data["contestant"];
            results[contestant_name] = result;
            
            data["data"][contestant_name]["author"] = result["author"];
            data["data"][contestant_name]["bot_name"] = result["bot_name"];
            
            if(results["contestant1"] != null && results["contestant2"] != null)
            {
                this.ProcessingComplete(data["data"]);
            } 
        }
        
        public function onFail(event:FaultEvent)
        {
            var msg = event.message;
        }
    }
}